home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / ODF Release 3 / ODFDev / ODF / OS / FWGraphx / FWShpLst.cpp < prev    next >
Encoding:
Text File  |  1996-12-16  |  15.4 KB  |  542 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWShpLst.cpp
  4. //    Release Version:    $ ODF 3 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9. //    The top shape is the first one in the list. The bottom shape is the last one. 
  10. //    FW_CShapeListShape draws the shape from the last (bottom) to the first (top)
  11.  
  12. #include "FWOS.hpp"
  13.  
  14. #ifndef FWSHPLST_H
  15. #include "FWShpLst.h"
  16. #endif
  17.  
  18. #ifndef FWSHAPE_H
  19. #include "FWShape.h"
  20. #endif
  21.  
  22. // ----- Foundation Includes -----
  23.  
  24. #ifndef FWSTREAM_H
  25. #include "FWStream.h"
  26. #endif
  27.  
  28. //========================================================================================
  29. //    RunTime Info
  30. //========================================================================================
  31.  
  32. #ifdef FW_BUILD_MAC
  33. #pragma segment FWGraphx_ShapeList
  34. #endif
  35.  
  36. //========================================================================================
  37. //    Template Instantiations
  38. //========================================================================================
  39.  
  40. #include "FWTColl.tpp"
  41.  
  42. FW_DEFINE_AUTO_TEMPLATE(FW_TOrderedCollection, FW_CShape)
  43.  
  44. #ifdef FW_USE_TEMPLATE_PRAGMAS
  45.     #pragma template_access public
  46.     #pragma template FW_TOrderedCollection<FW_CShape>
  47. #else
  48.     template class FW_TOrderedCollection<FW_CShape>;
  49. #endif
  50.  
  51. //========================================================================================
  52. //    class FW_CShapeListRep
  53. //========================================================================================
  54.  
  55. #ifdef FW_USE_TEMPLATE_PRAGMAS
  56. #pragma template_access public
  57. #pragma template FW_TOrderedCollection<FW_CShape>
  58. #endif
  59.  
  60. FW_DEFINE_AUTO(FW_CShapeListIterator)
  61. FW_DEFINE_AUTO(FW_CShapeListRep)
  62.  
  63. //----------------------------------------------------------------------------------------
  64. // FW_CShapeListRep::FW_CShapeListRep
  65. //----------------------------------------------------------------------------------------
  66.  
  67. FW_CShapeListRep::FW_CShapeListRep() :
  68.     fShapeList(NULL),
  69.     fValidAnchorPoint(FALSE)
  70. {
  71.     fShapeList = FW_NEW(FW_TOrderedCollection<FW_CShape>, ());
  72.     
  73.     FW_END_CONSTRUCTOR
  74. }
  75.  
  76. //----------------------------------------------------------------------------------------
  77. // FW_CShapeListRep::FW_CShapeListRep
  78. //----------------------------------------------------------------------------------------
  79.  
  80. FW_CShapeListRep::FW_CShapeListRep(const FW_CShapeListRep& other) :
  81.     fShapeList(NULL),
  82.     fValidAnchorPoint(FALSE)
  83. {
  84.     fShapeList = FW_NEW(FW_TOrderedCollection<FW_CShape>, ());
  85.  
  86.     FW_TRY
  87.     {
  88.         FW_CShapeListIterator ite(other);
  89.         for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  90.             AdoptAtBottom(shape->Copy());
  91.     }
  92.     FW_CATCH_BEGIN
  93.     FW_CATCH_EVERYTHING()
  94.     {
  95.         PrivFree();
  96.         FW_THROW_SAME();
  97.     }
  98.     FW_CATCH_END
  99.     
  100.     FW_END_CONSTRUCTOR
  101. }
  102.  
  103. //----------------------------------------------------------------------------------------
  104. // FW_CShapeListRep::FW_CShapeListRep
  105. //----------------------------------------------------------------------------------------
  106.  
  107. FW_CShapeListRep::FW_CShapeListRep(FW_CReadableStream& stream) :
  108.     fShapeList(NULL),
  109.     fValidAnchorPoint(FALSE)
  110. {
  111.     fShapeList = FW_NEW(FW_TOrderedCollection<FW_CShape>, ());
  112.     
  113.     FW_TRY
  114.     {
  115.         unsigned long count;
  116.         stream >> count;
  117.         
  118.         while(count -- != 0)
  119.         {
  120.             FW_CShape* shape;
  121.             FW_READ_DYNAMIC_OBJECT(stream, &shape, FW_CShape);
  122.  
  123.             AdoptAtBottom(shape);
  124.         }
  125.     }
  126.     FW_CATCH_BEGIN
  127.     FW_CATCH_EVERYTHING()
  128.     {
  129.         PrivFree();
  130.         FW_THROW_SAME();
  131.     }
  132.     FW_CATCH_END
  133.  
  134.     FW_END_CONSTRUCTOR
  135. }
  136.  
  137. //----------------------------------------------------------------------------------------
  138. // FW_CShapeListRep::~FW_CShapeListRep
  139. //----------------------------------------------------------------------------------------
  140.  
  141. FW_CShapeListRep::~FW_CShapeListRep()
  142. {
  143.     FW_START_DESTRUCTOR
  144.  
  145.     PrivFree();
  146. }
  147.  
  148. //----------------------------------------------------------------------------------------
  149. // FW_CShapeListRep::DeleteAll
  150. //----------------------------------------------------------------------------------------
  151.  
  152. void FW_CShapeListRep::DeleteAll()
  153. {
  154.     if (fShapeList)
  155.     {
  156.         FW_CShape* shape;
  157.         while ((shape = fShapeList->First()) != NULL)
  158.         {
  159.             Remove(shape);
  160.             delete shape;        
  161.         }
  162.     }
  163. }
  164.  
  165. //----------------------------------------------------------------------------------------
  166. // FW_CShapeListRep::PrivFree
  167. //----------------------------------------------------------------------------------------
  168.  
  169. void FW_CShapeListRep::PrivFree()
  170. {
  171.     DeleteAll();
  172.     delete fShapeList;
  173.     fShapeList = NULL;
  174. }
  175.  
  176. //----------------------------------------------------------------------------------------
  177. // FW_CShapeListRep::GetCount
  178. //----------------------------------------------------------------------------------------
  179.  
  180. unsigned long FW_CShapeListRep::GetCount() const
  181. {
  182.     return fShapeList->Count();
  183. }
  184.  
  185. //----------------------------------------------------------------------------------------
  186. // FW_CShapeListRep::Purge
  187. //----------------------------------------------------------------------------------------
  188.  
  189. void FW_CShapeListRep::Purge()
  190. {
  191.     FW_CShapeListIterator ite(*this);
  192.     for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  193.     {
  194.         shape->Purge();
  195.     }
  196. }
  197.  
  198. //----------------------------------------------------------------------------------------
  199. // FW_CShapeListRep::AdoptAtTop
  200. //----------------------------------------------------------------------------------------
  201.  
  202. void FW_CShapeListRep::AdoptAtTop(FW_CShape* shape)
  203. {
  204.     fShapeList->AddFirst(shape);
  205.     fValidAnchorPoint = FALSE;
  206. }
  207.  
  208. //----------------------------------------------------------------------------------------
  209. // FW_CShapeListRep::AdoptAtBottom
  210. //----------------------------------------------------------------------------------------
  211.  
  212. void FW_CShapeListRep::AdoptAtBottom(FW_CShape* shape)
  213. {
  214.     fShapeList->AddLast(shape);
  215.     fValidAnchorPoint = FALSE;
  216. }
  217.  
  218. //----------------------------------------------------------------------------------------
  219. // FW_CShapeListRep::AdoptBelow
  220. //----------------------------------------------------------------------------------------
  221.  
  222. void FW_CShapeListRep::AdoptBelow(FW_CShape* shapeToAdd, FW_CShape* belowWhich)
  223. {
  224.     fShapeList->AddAfter(belowWhich, shapeToAdd);
  225.     fValidAnchorPoint = FALSE;
  226. }
  227.  
  228. //----------------------------------------------------------------------------------------
  229. // FW_CShapeListRep::AdoptAbove
  230. //----------------------------------------------------------------------------------------
  231.  
  232. void FW_CShapeListRep::AdoptAbove(FW_CShape* shapeToAdd, FW_CShape* aboveWhich)
  233. {
  234.     fShapeList->AddBefore(aboveWhich, shapeToAdd);
  235.     fValidAnchorPoint = FALSE;
  236. }
  237.  
  238. //----------------------------------------------------------------------------------------
  239. // FW_CShapeListRep::Contains
  240. //----------------------------------------------------------------------------------------
  241.  
  242. FW_Boolean FW_CShapeListRep::Contains(FW_CShape* shape) const
  243. {    
  244.     return fShapeList->Contains(shape);
  245. }
  246.  
  247. //----------------------------------------------------------------------------------------
  248. // FW_CShapeListRep::Remove
  249. //----------------------------------------------------------------------------------------
  250.  
  251. void FW_CShapeListRep::Remove(FW_CShape* shape)
  252. {
  253.     fShapeList->Remove(shape);
  254.     fValidAnchorPoint = FALSE;
  255. }
  256.  
  257. //----------------------------------------------------------------------------------------
  258. // FW_CShapeListRep::RemoveAll
  259. //----------------------------------------------------------------------------------------
  260.  
  261. void FW_CShapeListRep::RemoveAll()
  262. {
  263.     while (GetCount() != 0)
  264.         this->Remove(fShapeList->First());
  265. }
  266.  
  267. //----------------------------------------------------------------------------------------
  268. // FW_CShapeListRep::RemoveTop
  269. //----------------------------------------------------------------------------------------
  270.  
  271. FW_CShape* FW_CShapeListRep::RemoveTop()
  272. {
  273.     FW_ASSERT(GetCount() != 0);
  274.     fValidAnchorPoint = FALSE;
  275.     return fShapeList->RemoveFirst();
  276. }
  277.  
  278. //----------------------------------------------------------------------------------------
  279. // FW_CShapeListRep::RemoveBottom
  280. //----------------------------------------------------------------------------------------
  281.  
  282. FW_CShape* FW_CShapeListRep::RemoveBottom()
  283. {
  284.     FW_ASSERT(GetCount() != 0);
  285.     fValidAnchorPoint = FALSE;
  286.     return fShapeList->RemoveLast();
  287. }
  288.  
  289. //----------------------------------------------------------------------------------------
  290. // FW_CShapeListRep::MoveUp
  291. //----------------------------------------------------------------------------------------
  292.  
  293. FW_Boolean FW_CShapeListRep::MoveUp(FW_CShape* shape)
  294. {
  295.     FW_CShape* before = fShapeList->Before(shape);
  296.     if (before != NULL)
  297.     {
  298.         fShapeList->Remove(shape);
  299.         fShapeList->AddBefore(before, shape);
  300.         return TRUE;
  301.     }
  302.  
  303.     return FALSE;
  304. }
  305.  
  306. //----------------------------------------------------------------------------------------
  307. // FW_CShapeListRep::MoveToTop
  308. //----------------------------------------------------------------------------------------
  309.  
  310. FW_Boolean FW_CShapeListRep::MoveToTop(FW_CShape* shape)
  311. {
  312.     if (fShapeList->Before(shape) != NULL)
  313.     {
  314.         fShapeList->Remove(shape);
  315.         fShapeList->AddFirst(shape);
  316.         return TRUE;
  317.     }
  318.  
  319.     return FALSE;
  320. }
  321.  
  322. //----------------------------------------------------------------------------------------
  323. // FW_CShapeListRep::MoveDown
  324. //----------------------------------------------------------------------------------------
  325.  
  326. FW_Boolean FW_CShapeListRep::MoveDown(FW_CShape* shape)
  327. {
  328.     FW_CShape* after = fShapeList->After(shape);
  329.     if (after != NULL)
  330.     {
  331.         fShapeList->Remove(shape);
  332.         fShapeList->AddAfter(after, shape);
  333.         return TRUE;
  334.     }
  335.  
  336.     return FALSE;
  337. }
  338.  
  339. //----------------------------------------------------------------------------------------
  340. // FW_CShapeListRep::MoveToBottom
  341. //----------------------------------------------------------------------------------------
  342.  
  343. FW_Boolean FW_CShapeListRep::MoveToBottom(FW_CShape* shape)
  344. {
  345.     if (fShapeList->After(shape) != NULL)
  346.     {
  347.         fShapeList->Remove(shape);
  348.         fShapeList->AddLast(shape);
  349.         return TRUE;
  350.     }
  351.  
  352.     return FALSE;
  353. }
  354.  
  355. //----------------------------------------------------------------------------------------
  356. // FW_CShapeListRep::Write
  357. //----------------------------------------------------------------------------------------
  358.  
  359. void FW_CShapeListRep::Write(FW_CWritableStream& stream) const
  360. {
  361.     unsigned long count = this->GetCount();
  362.     stream << count;
  363.     
  364.     FW_CShapeListIterator ite(*this);
  365.     for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  366.         FW_WRITE_DYNAMIC_OBJECT(stream, shape, FW_CShape);
  367. }
  368.  
  369. //----------------------------------------------------------------------------------------
  370. // FW_CShapeListRep::IsEqual
  371. //----------------------------------------------------------------------------------------
  372.  
  373. FW_Boolean FW_CShapeListRep::IsEqual(const FW_CShapeListRep* rep) const
  374. {
  375.     if (rep == this)
  376.         return TRUE;
  377.     
  378.     unsigned long count = this->GetCount();
  379.         
  380.     if(count == rep->GetCount())
  381.     {
  382.         FW_CShapeListIterator iteThis(*this);
  383.         FW_CShapeListIterator iteThat(*rep);
  384.         
  385.         FW_CShape* shapeThis = iteThis.First();
  386.         FW_CShape* shapeThat = iteThat.First();
  387.         
  388.         for (unsigned long i = 0; i< count; ++ i)
  389.         {
  390.             if(shapeThis != shapeThat)
  391.                 return FALSE;
  392.             
  393.             shapeThis = iteThis.Next();
  394.             shapeThat = iteThat.Next();    
  395.         }
  396.         return TRUE;
  397.     }
  398.  
  399.     return FALSE;
  400. }
  401.  
  402. //----------------------------------------------------------------------------------------
  403. //    FW_CShapeListRep::GetAnchorPoint
  404. //----------------------------------------------------------------------------------------
  405.  
  406. FW_CPoint FW_CShapeListRep::GetAnchorPoint() const
  407. {
  408.     if (fValidAnchorPoint)
  409.     {
  410.         return fAnchorPoint;
  411.     }
  412.     else
  413.     {
  414.         FW_Boolean first = TRUE;
  415.         FW_CPoint anchor, anAnchor;
  416.     
  417.         FW_CShapeListIterator ite(*this);
  418.         for (FW_CShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  419.         {
  420.             anAnchor = shape->GetAnchorPoint();
  421.             if (first)
  422.                 anchor = anAnchor;
  423.             else
  424.             {
  425.                 anchor.x = FW_Minimum(anchor.x, anAnchor.x);
  426.                 anchor.y = FW_Minimum(anchor.y, anAnchor.y);
  427.             }
  428.             first = FALSE;
  429.         }    
  430.     
  431.         ((FW_CShapeListRep*)this)->fAnchorPoint = anchor;
  432.         ((FW_CShapeListRep*)this)->fValidAnchorPoint = TRUE;
  433.         
  434.         return anchor;
  435.     }
  436. }
  437.  
  438. //========================================================================================
  439. //    class FW_PShapeList
  440. //========================================================================================
  441.  
  442. FW_DEFINE_AUTO_TEMPLATE(FW_TCountedPtr, FW_CShapeListRep)
  443.  
  444. #ifdef FW_USE_TEMPLATE_PRAGMAS
  445.  
  446. #pragma template_access public
  447. #pragma template FW_TCountedPtr<FW_CShapeListRep>
  448.  
  449. #else
  450.  
  451. template class FW_TCountedPtr<FW_CShapeListRep>;
  452.  
  453. #endif
  454.  
  455. //----------------------------------------------------------------------------------------
  456. // FW_PShapeList::FW_PShapeList
  457. //----------------------------------------------------------------------------------------
  458.  
  459. FW_PShapeList::FW_PShapeList() :
  460.     FW_TCountedPtr<FW_CShapeListRep>(FW_NEW(FW_CShapeListRep, ()))
  461. {
  462. }
  463.  
  464. //----------------------------------------------------------------------------------------
  465. // FW_PShapeList::FW_PShapeList
  466. //----------------------------------------------------------------------------------------
  467.  
  468. FW_PShapeList::FW_PShapeList(FW_CReadableStream& stream) :
  469.     FW_TCountedPtr<FW_CShapeListRep>(FW_NEW(FW_CShapeListRep, (stream)))
  470. {
  471. }
  472.  
  473. //----------------------------------------------------------------------------------------
  474. // FW_PShapeList::~FW_PShapeList
  475. //----------------------------------------------------------------------------------------
  476.  
  477. FW_PShapeList::~FW_PShapeList()
  478. {
  479. }
  480.  
  481. //----------------------------------------------------------------------------------------
  482. // FW_PShapeList::FW_PShapeList
  483. //----------------------------------------------------------------------------------------
  484.  
  485. FW_PShapeList::FW_PShapeList(const FW_PShapeList& other)
  486. {
  487.     SetRep(other.fRep);
  488. }
  489.  
  490. //----------------------------------------------------------------------------------------
  491. // FW_PShapeList::operator=
  492. //----------------------------------------------------------------------------------------
  493.  
  494. FW_PShapeList& FW_PShapeList::operator=(const FW_PShapeList& other)
  495. {
  496.     FW_TCountedPtr<FW_CShapeListRep>::operator=(other);
  497.     return *this;
  498. }
  499.  
  500. //----------------------------------------------------------------------------------------
  501. // FW_PShapeList::Copy
  502. //----------------------------------------------------------------------------------------
  503.  
  504. FW_PShapeList FW_PShapeList::Copy() const
  505. {
  506.     FW_PShapeList list;
  507.     list.SetRep(FW_NEW(FW_CShapeListRep, (*fRep)));
  508.     return list;
  509. }
  510.  
  511. //----------------------------------------------------------------------------------------
  512. //    operator <<
  513. //----------------------------------------------------------------------------------------
  514.  
  515. FW_CWritableStream& operator << (FW_CWritableStream& stream, const FW_PShapeList& list)
  516. {
  517.     list.fRep->Write(stream);
  518.     return stream;
  519. }
  520.  
  521. //----------------------------------------------------------------------------------------
  522. //    operator >>
  523. //----------------------------------------------------------------------------------------
  524.  
  525. FW_CReadableStream& operator >> (FW_CReadableStream& stream, FW_PShapeList& list)
  526. {
  527.     list.SetRep(FW_NEW(FW_CShapeListRep, (stream)));
  528.     return stream;
  529. }
  530.  
  531. //========================================================================================
  532. //    class FW_CShapeListIterator
  533. //========================================================================================
  534.  
  535. //----------------------------------------------------------------------------------------
  536. // FW_CShapeListIterator::~FW_CShapeListIterator
  537. //----------------------------------------------------------------------------------------
  538. FW_CShapeListIterator::~FW_CShapeListIterator()
  539. {
  540. }
  541.  
  542.